<?php
	
	/**
	 * MVC Autoloader File.
	 * 
	 * This files autoloads all the controllers, models and plugins
	 * 
	 * @author Brian Bal <brian@youbeqb.com>
	 * @version 1.0
	 * @package MVC
	 */
	
	// Include System Files:
	require(SYSTEM_DIR . "pk_cache.php");
	require(SYSTEM_DIR . "pk_model.php");
	require(SYSTEM_DIR . "pk_gateway.php");
	require(SYSTEM_DIR . "pk_view.php");
	require(SYSTEM_DIR . "pk_controller.php");
	require(SYSTEM_DIR . "pk_plugin.php");
	require(SYSTEM_DIR . "pk_mailer.php");
	require(SYSTEM_DIR . "pk_router.php");
	require(SYSTEM_DIR . "pk_database.php");
	require(SYSTEM_DIR . "pk_core.php");
	
	// Include System Library Files
	require(SYSTEM_DIR . "libraries/helpers.library.php");
	require(SYSTEM_DIR . "libraries/flash_messages.library.php");
	
	// Include the Mailer Library if needed
	if(defined("MAIL_TYPE"))
	{
		require(SYSTEM_DIR . "libraries/Swift/lib/Swift.php");
		
		// included the SMTP connector
		if(MAIL_TYPE == "SMTP")
		{
			require(SYSTEM_DIR . "libraries/Swift/lib/Swift/Connection/SMTP.php");
		}
		
		// include the Sendmail connector
		else if(MAIL_TYPE == "Sendmail")
		{
			require(SYSTEM_DIR . "libraries/Swift/lib/Swift/Connection/Sendmail.php");
		}
		
		// include the Native PHP mail connector
		else
		{
			require(SYSTEM_DIR . "libraries/Swift/lib/Swift/Connection/NativeMail.php");
		}
	}
	
	// Include Plugin Files:
	if(! isset($plugins))
	{
		$plugins = array();
	}
	
	if(! isset($routes))
	{
		$routes = array(
			array("url" => "", "controller" => "welcome", "action" => "index"),

			array("url" => "admin/:controller/:action/:id"),
			array("url" => "admin/:controller/:action"),
			array("url" => "admin/:controller"),

			array("url" => ":controller/:action/:id"),
			array("url" => ":controller/:action"),
			array("url" => ":controller")
		);
	}
	
	if(! defined("NO_CORE"))
	{
		// Start up the Core:
		$pk_core = new PkCore($plugins, $routes);
	}else{
		// connect to the database
		$pk_database = PkDatabase::getPkDatabase();
		$pk_database->connectToDatabase(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
	}
	
	
	// Autoload the Classes
	function __autoload($class_name)
	{
		$file = getFilePath($class_name);
		
		if (file_exists($file) == false) {
			trigger_error("Autoloader: failed to load $class_name ($file)");
		}else{
			include ($file);
		}
	}
	
	function getFilePath($class_name)
	{
		$dir_name = PROJECT_DIR . "app" . DS . "models";
		
		// change dir name and file name for controller files
		if(strripos($class_name, "Controller"))
		{
			$dir_name = PROJECT_DIR . "app" . DS . 'controllers';
			
			$filename = str_ireplace("controller", "", strtolower($class_name)) . '.php';
			$file = $dir_name . DS . $filename;
		}
		
		// get the file path for a mailer controller
		else if(preg_match("/Mailer$/", $class_name))
		{
			$dir_name = PROJECT_DIR . "app" . DS . 'mailers';
			
			$filename = uncamelize($class_name) . '.php';
			$file = $dir_name . DS . $filename;
			
			//echo "<p>mailer file name: ($file)</p>\n\n";
		}
		
		// get file name for other files
		else
		{
			$filename = preg_replace("/[A-Z]/", "_\\0", $class_name);
			$filename = substr($filename, 1, strlen($filename));
			$filename = strtolower($filename) . '.php';
			//echo "<p>Camel to unserscored: ($class_name) vs ($filename)</p>";
			
			$file = $dir_name . DS . $filename;
		}
		
		return $file;
	}
	
	
?>